﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using CommunitySystem.GuaZhiDuanLianService;
using System.Windows.Data;
using EHR.Common;

namespace CommunitySystem.PageView
{
    public partial class JYListControl : UserControl
    {
        private GuaZhiDuanLianServiceClient client = new GuaZhiDuanLianServiceClient();
        public JYListControl()
        {
            InitializeComponent();

            IsWorking.Visibility = Visibility.Visible;
            busy.BusyContent = "正在同步数据，请稍后...";

            client.GetJYCompleted += new EventHandler<GetJYCompletedEventArgs>(client_GetJYCompleted);
            client.GetJYCountCompleted += new EventHandler<GetJYCountCompletedEventArgs>(client_GetJYCountCompleted);
       
            Query();
        }
        private int pageSize = 20;
        void client_GetJYCountCompleted(object sender, GetJYCountCompletedEventArgs e)
        {
            ObservableCollection<int> list = new ObservableCollection<int>();
            int last = e.Result % pageSize;
            int count = 0;
            if (last > 0)
                count = e.Result / pageSize + 1;
            else
                count = e.Result / pageSize;
            for (int i = 0; i < count; i++)
            {
                list.Add(i);
            }

            PagedCollectionView pcv = new PagedCollectionView(list);
            dataPager1.PageSize = 1;
            dataPager1.Source = null;
            dataPager1.Source = pcv;
        }

        void client_GetJYCompleted(object sender, GetJYCompletedEventArgs e)
        {
            dg.ItemsSource = null;
            dg.ItemsSource = e.Result;
            IsWorking.Visibility = Visibility.Collapsed;
        }
        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            JYWindow win = new JYWindow();
            win.Show();
            win.Closed += new EventHandler(win_Closed);
        }

        void win_Closed(object sender, EventArgs e)
        {
            Query();
        }
        private void Query() 
        {
            selectedempmodel.Clear();
            string tj = string.Empty;
            if (Session.LoginUser.RoleID != 1)
                tj = string.Format(" and emp.createuser='{0}'",Session.LoginUser.LoginName);
            client.GetJYCountAsync(tj);
        }
        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (dg.ItemsSource != null)
            {
                foreach (Emp_JYModel i in (ObservableCollection<Emp_JYModel>)dg.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.Id == i.Id).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.Id == i.Id).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.Id == i.Id).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                if (selectedempmodel.Count > 1)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("只能选择一条进行编辑！");
                    return;
                }
                if (selectedempmodel.FirstOrDefault().Status != "借用中")
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("只能对借用中的信息进行编辑！");
                    return;
                }
                JYWindow win = new JYWindow(selectedempmodel.FirstOrDefault());
                win.Show();
                win.Closed += new EventHandler(win_Closed);
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的信息！"); 
         
        }
        private void btnDele_Click(object sender, RoutedEventArgs e)
        {
            if (dg.ItemsSource != null)
            {
                foreach (Emp_JYModel i in (ObservableCollection<Emp_JYModel>)dg.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.Id == i.Id).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.Id == i.Id).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.Id == i.Id).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {

                string ids = "";
                foreach (var item in selectedempmodel)
                {
                    if (item.Status != "借用中")
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("只能对借用中的信息进行删除！");
                        return;
                    }
                    ids += item.Id + ",";
                }
                if (MessageBox.Show("确定要删除该信息吗？", "系统提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    client.JYDelCompleted -= new EventHandler<JYDelCompletedEventArgs>(client_JYDelCompleted);
                    client.JYDelCompleted += new EventHandler<JYDelCompletedEventArgs>(client_JYDelCompleted);
                    client.JYDelAsync(ids.TrimEnd(','));
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要删除的方案信息！"); 
        }

        void client_JYDelCompleted(object sender, JYDelCompletedEventArgs e)
        {
            if (e.Result)
                SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");

            else
                SilverlightMessageBoxLibrary.Message.InfoMessage("删除失败！");

            Query();
        }
        private void btnJSJY_Click(object sender, RoutedEventArgs e)
        {
            if (dg.ItemsSource != null)
            {
                foreach (Emp_JYModel i in (ObservableCollection<Emp_JYModel>)dg.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.Id == i.Id).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.Id == i.Id).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.Id == i.Id).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                if (selectedempmodel.Count > 1)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("只能选择一条进行借用结束操作！");
                    return;
                }
                if (selectedempmodel.FirstOrDefault().Status != "借用中")
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("只能对借用中的信息进行借用结束！");
                    return;
                }
            
                JSJYWindow win = new JSJYWindow(selectedempmodel.FirstOrDefault());
                win.Show();
                win.Closed += new EventHandler(win_Closed);
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要借用结束的信息！"); 
          
        }
        /// <summary>
        /// 多选
        /// </summary>
        /// <param name="sender"></param>ii
        /// <param name="e"></param>
        public ObservableCollection<Emp_JYModel> selectedempmodel =
         new ObservableCollection<Emp_JYModel>();
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (Emp_JYModel p in dg.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.Id == p.Id).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.Id == p.Id).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.Id == p.Id).FirstOrDefault());
                    }
                }
            }
        }
        private void dataPager1_PageIndexChanged(object sender, EventArgs e)
        {
            int index = dataPager1.PageIndex == -1 ? 0 : dataPager1.PageIndex;
            string tj = string.Empty;
            if (Session.LoginUser.RoleID != 1)
                tj = string.Format(" and emp.createuser='{0}'", Session.LoginUser.LoginName);
            client.GetJYAsync(tj, index + 1, pageSize);
        }
    }
}
